package com.vitabuybuy.tv.dialog

import android.content.Context
import android.text.Html
import android.view.View
import android.view.animation.Animation
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.library.common.util.DateUtil
import com.vitabuybuy.tv.adaptebinding.ImageAdapter
import com.library.common.widget.BGButton
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.dto.GroupBuyMemberDto
import razerdp.basepopup.BasePopupWindow
import razerdp.util.animation.AnimationHelper
import razerdp.util.animation.ScaleConfig

/**
 * 参加拼单弹出框
 */
class JoinGroupPopup(
    context: Context,
    memberDto: GroupBuyMemberDto,
    onClickJoin:(memberDto: GroupBuyMemberDto) ->Unit
) : BasePopupWindow(context) {
    private lateinit var ivClose: ImageView
    private lateinit var ivGroupHead: ImageView
    private lateinit var tvShortOfSuccess: TextView
    private lateinit var tvRemainingTime: TextView
    private lateinit var btnGoGroup: BGButton

    override fun onCreateContentView(): View {
        return createPopupById(R.layout.popup_join_group)
    }

    init {
        ImageAdapter.setCircleImageUrl(
            ivGroupHead,
            memberDto.creator.avatar,
            ContextCompat.getDrawable(context, R.drawable.wode_icon_touxiang)
        )
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            tvShortOfSuccess.text = Html.fromHtml(
                String.format(context.getString(R.string.short_of_success), (memberDto.memberLimit - memberDto.memberQuantity)),
                Html.FROM_HTML_MODE_COMPACT
            )
        }
        tvRemainingTime.text = String.format(context.getString(R.string.remaining_time, DateUtil.formatSeconds(memberDto.remainingTime)))
        btnGoGroup.setOnClickListener {
            onClickJoin(memberDto)
            dismiss()
        }
    }

    override fun onViewCreated(contentView: View) {
        super.onViewCreated(contentView)

        ivClose = findViewById(R.id.ivClose)
        ivGroupHead = findViewById(R.id.ivGroupHead)
        tvShortOfSuccess = findViewById(R.id.tvShortOfSuccess)
        tvRemainingTime = findViewById(R.id.tvRemainingTime)
        btnGoGroup = findViewById(R.id.btnGoGroup)

        setViewClickListener(View.OnClickListener { this.dismiss() }, ivClose)
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            tvShortOfSuccess.text = Html.fromHtml(
                String.format(context.getString(R.string.short_of_success_time), 1),
                Html.FROM_HTML_MODE_COMPACT
            )
        } else {
            tvShortOfSuccess.text =
                Html.fromHtml(String.format(context.getString(R.string.short_of_success_time), 1))
        }
        tvRemainingTime.text = "23 : 21 : 27"
    }

    override fun onCreateShowAnimation(): Animation {
        return AnimationHelper.asAnimation()
            .withScale(ScaleConfig.CENTER)
            .toShow()
    }

    override fun onCreateDismissAnimation(): Animation {
        return AnimationHelper.asAnimation()
            .withScale(ScaleConfig.CENTER)
            .toDismiss()
    }
}